package com.wendy.common.core.json.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import com.wendy.common.core.utils.DesensitizedUtils;
import lombok.extern.log4j.Log4j2;

import java.io.IOException;

/**
 * Standard serializer used for {@link MyAnnotationIntrospector} types.
 *<p>
 * Based on {@link StdScalarSerializer} since the JSON value is
 * scalar (String).
 */
@JacksonStdImpl
@Log4j2
public class MyDataMaskingSerializer extends JsonSerializer<String>
{
    private  DataMaskingEnum dataMaskingEnum;
    private  int prefixNoMaskLen;
    private  int suffixNoMaskLen;
    private  String mask;

    public MyDataMaskingSerializer(DataMaskingEnum dataMaskingEnum,int prefixNoMaskLen,int suffixNoMaskLen,String mask) {
        this.dataMaskingEnum = dataMaskingEnum;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if(DataMaskingEnum.CUSTOMER.equals(dataMaskingEnum)){
            DesensitizedUtils.desValue(value,prefixNoMaskLen,suffixNoMaskLen,mask);
        }else if(DataMaskingEnum.MOBILE_PHONE.equals(dataMaskingEnum)){
            DesensitizedUtils.mobilePhone(value);
        }else if(DataMaskingEnum.CHINESE_NAME.equals(dataMaskingEnum)){
            DesensitizedUtils.chineseName(value);
        }else if(DataMaskingEnum.PASSWORD.equals(dataMaskingEnum)){
            DesensitizedUtils.password(value);
        }else if(DataMaskingEnum.EMAIL.equals(dataMaskingEnum)){
            DesensitizedUtils.email(value);
        }else if(DataMaskingEnum.TELEPHONE.equals(dataMaskingEnum)){
            DesensitizedUtils.telephone(value);
        }else if(DataMaskingEnum.BANK_CARD.equals(dataMaskingEnum)){
            DesensitizedUtils.bankCard(value);
        }else if(DataMaskingEnum.ADDRESS.equals(dataMaskingEnum)){
            DesensitizedUtils.address(value);
        }



    }
}
